//
//  ZSliderView.swift
//  ZPlayer
//
//  Created by zuber on 2018/11/14.
//  Copyright © 2018年 zuber. All rights reserved.
//

import UIKit
import SnapKit

protocol ZSliderViewDelegate: class {
  func zsliderView(_ view: ZSliderView, slideTo percent: CGFloat)
  func zsliderViewBeginSlide(_ view: ZSliderView)
}


class ZSliderView: UIView {
  
  private var backTrackView = UIView()
  private var loadTrackLayer = CALayer()
  private var playedTrackLayer = CALayer()
  private var sliderView = UIView()
  private var sliderLayer = CALayer()
  private let trackHeight: CGFloat = 5
  private var sliderWidth: CGFloat = 0
  
  weak var delegate: ZSliderViewDelegate?
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.prepareView()
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.prepareView()
  }
  
  func prepareView(){
    addSubview(backTrackView)
    backTrackView.backgroundColor = UIColor.black
    backTrackView.layer.cornerRadius = 2.5
    backTrackView.layer.masksToBounds = true
    backTrackView.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.centerY.equalToSuperview()
      make.height.equalTo(trackHeight)
    }
    
    loadTrackLayer.backgroundColor = UIColor.coverLoadedgroundColor.cgColor
    playedTrackLayer.backgroundColor = UIColor.coverPlayedgroundColor.cgColor
    backTrackView.layer.addSublayer(loadTrackLayer)
    backTrackView.layer.addSublayer(playedTrackLayer)
    
    sliderLayer.backgroundColor = UIColor.white.cgColor
    sliderLayer.cornerRadius = 6
    sliderLayer.masksToBounds = true
    addSubview(sliderView)
    sliderView.layer.addSublayer(sliderLayer)
    sliderLayer.frame = CGRect(x: 0, y: 4, width: 12, height: 12)
    let panSlider = UIPanGestureRecognizer(target: self, action: #selector(panSliderView(_:)))
    sliderView.isUserInteractionEnabled = true
    sliderView.addGestureRecognizer(panSlider)
    
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    sliderWidth = self.bounds.width
    sliderView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    loadTrackLayer.frame = CGRect(x: 0, y: 0, width: 6, height: trackHeight)
    playedTrackLayer.frame = CGRect(x: 0, y: 0, width: 6, height: trackHeight)
  }
  
  func setLoadedPercent(_ percent: CGFloat){
    let value = max(min(1,percent),0)
    let width = self.bounds.width
    let loadedWidth = width*value
    self.loadTrackLayer.frame.size.width = loadedWidth
  }
  
  func setPlayedPercent(_ percent: CGFloat){
    let value = max(min(1,percent),0)
    let width = self.bounds.width
    let loadedWidth = width*value
    playedTrackLayer.frame.size.width = loadedWidth
    if !isSliding {
      sliderView.frame.origin.x = max(loadedWidth - 10,0)
    }
  }
  
  private var panOriginX: CGFloat = 0
  private var sliderOriginX: CGFloat = 0
  var isSliding = false
  
  @objc
  func panSliderView(_ gesture: UIPanGestureRecognizer){
    let location = gesture.location(in: self)
    
    switch gesture.state {
    case .began:
      panOriginX = location.x
      sliderOriginX = sliderView.frame.origin.x
      isSliding = true
      delegate?.zsliderViewBeginSlide(self)
    case .changed:
      let newX = location.x
      let distance = newX - panOriginX
      let percent = CGFloat(distance)/sliderWidth
      sliderView.frame.origin.x = sliderOriginX + sliderWidth*percent
    case .cancelled,.ended,.failed:
      let newX = location.x
      let distance = newX - panOriginX
      let percent = CGFloat(distance)/sliderWidth
      sliderView.frame.origin.x = sliderOriginX + sliderWidth*percent
      let newPercent = (sliderOriginX + sliderWidth*percent)/sliderWidth
      delegate?.zsliderView(self, slideTo: newPercent)
    default:
      break
    }
  }
  
}
